package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.ctsi.ssdc.service.LitemallUserService;
import com.ctsi.ssdc.domain.LitemallUser;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallUser.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-user-controller"})
@RestController
@RequestMapping("/api")
public class LitemallUserController {

    private final Logger log = LoggerFactory.getLogger(LitemallUserController.class);

    private static final String ENTITY_NAME = "litemallUser";

    private final LitemallUserService litemallUserService;

    public LitemallUserController(LitemallUserService litemallUserService) {
        this.litemallUserService = litemallUserService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallUsers : Create a new litemallUser.
     *
     * @param litemallUser the litemallUser to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallUser,
        or with status 400 (Bad Request) if the litemallUser has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallUser",name = "litemallUser",value = "the litemallUser to create")
    })
    @ApiOperation(value = "POST  /litemallUsers : create a new litemallUser.",
        notes = "POST  /litemallUsers : create a new litemallUser.",httpMethod = "POST")
    @PostMapping("/litemallUsers")
    public ResponseEntity<LitemallUser> createLitemallUser(@RequestBody LitemallUser litemallUser) throws URISyntaxException {
        log.debug("REST request to save LitemallUser : {}", litemallUser);
        if (litemallUser.getId() != null) {
            throw new BadRequestAlertException("A new litemallUser cannot already have an id", ENTITY_NAME, "idexists");
        }

        boolean save =  litemallUserService.save(litemallUser);
        Assert.isTrue(save,"Insert failed");
        LitemallUser result=litemallUser;

        return ResponseEntity.created(new URI("/api/litemallUsers" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallUsers : Updates an existing litemallUser.
     *
     * @param litemallUser the litemallUser to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallUser,
     * or with status 400 (Bad Request) if the litemallUser is not valid,
     * or with status 500 (Internal Server Error) if the litemallUser couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallUser",name = "litemallUser",value = "the litemallUser to update")
    })
    @ApiOperation(value = "PUT  /litemallUsers : updates an existing litemallUser.",
        notes = "PUT  /litemallUsers : updates an existing litemallUser.",httpMethod = "PUT")
    @PutMapping("/litemallUsers")
    public ResponseEntity<LitemallUser> updateLitemallUser(@RequestBody LitemallUser litemallUser) throws URISyntaxException {
        log.debug("REST request to update LitemallUser : {}", litemallUser);
	    if (litemallUser.getId() == null) {
	    	return createLitemallUser(litemallUser);
	    }

        boolean update = litemallUserService.updateById(litemallUser);
        Assert.isTrue(update,"Update failed");
        LitemallUser result=litemallUser;

        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallUsers/:id : get the "id" litemallUser.
     *
     * @param id the id of the litemallUser to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallUser, or with status 404 (Not Found)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
        name = "id",value = "the id of the litemallUser to retrieve")
    })
    @ApiOperation(value = "GET  /litemallUsers/id : get the id litemallUser.",
        notes = "GET  /litemallUsers/id : get the id litemallUser.",httpMethod = "GET")
    @GetMapping("/litemallUsers/{id}")
    public ResponseEntity<LitemallUser> getLitemallUser(@PathVariable Integer id) {
        log.debug("REST request to get LitemallUser : {}", id);
        LitemallUser litemallUser = litemallUserService.getById(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallUser));
    }

    /**
    * GET  /litemallUsers : get the litemallUsers firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallUsers in body
    */
    @ApiOperation(value = "GET  /litemallUsers/id : get the id litemallUser.",
        notes = "GET  /litemallUsers/id : get the id litemallUser.",httpMethod = "GET")
    @GetMapping("/litemallUsers")
    public PageResult<LitemallUser> getLitemallUsersList(String username ,Pageable pageable) {
        log.debug("REST request to get LitemallUsersList");
        return litemallUserService.findFirstStringColumn(username ,pageable);
    }
	
    /**
     * DELETE  /litemallUsers/:id : delete the "id" litemallUser.
     *
     * @param id the id of the litemallUser to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
        value = "the id of the litemallUser to delete")
    })
    @ApiOperation(value = "DELETE  /litemallUsers/id : delete the id litemallUser.",
        notes = "DELETE  /litemallUsers/id : delete the id litemallUser.",httpMethod = "DELETE")
    @DeleteMapping("/litemallUsers/{id}")
    public ResponseEntity<Void> deleteLitemallUser(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallUser : {}", id);
        litemallUserService.removeById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
